package com.sinog.biz.service.business.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sinog.biz.config.param.Converse;
import com.sinog.biz.mapper.havexml.AbAnzhiMapper;
import com.sinog.biz.mapper.havexml.AbBangfuMapper;
import com.sinog.biz.mapper.havexml.AbCunJiandingMapper;
import com.sinog.biz.mapper.havexml.AbGenzongbangjiaoMapper;
import com.sinog.biz.mapper.havexml.AbJiuMapper;
import com.sinog.biz.mapper.havexml.AbLieguanMapper;
import com.sinog.biz.mapper.havexml.AbMvMapper;
import com.sinog.biz.mapper.havexml.AbPaichaMapper;
import com.sinog.biz.mapper.havexml.AbPrisonerMapper;
import com.sinog.biz.mapper.havexml.AbShehuijiuzhuMapper;
import com.sinog.biz.mapper.havexml.AbTuoguanMapper;
import com.sinog.biz.mapper.havexml.AbXiaozuMapper;
import com.sinog.biz.mapper.havexml.AbZyzjiuzhuMapper;
import com.sinog.biz.mapper.havexml.PlacementBaseInfoMapper;
import com.sinog.biz.mapper.havexml.PrisonerFxMapper;
import com.sinog.biz.mapper.havexml.PrisonerMainMapper;
import com.sinog.biz.mapper.havexml.ReleaseHelpEduMapper;
import com.sinog.biz.mapper.havexml.SystemCodeMapper;
import com.sinog.biz.mapper.havexml.UploadAppendixFileMapper;
import com.sinog.biz.service.business.BjdxMvInfoMainService;
import com.sinog.biz.service.business.SentenceService;
import com.sinog.biz.util.CodeUrlUtils;
import com.sinog.biz.util.IdGen;
import com.sinog.biz.util.JdbcUtils;
import com.sinog.biz.util.smlib.Sm4;
import com.sinog.core.domain.AbMvResultVo;
import com.sinog.core.domain.PlacementBaseInfo;
import com.sinog.core.domain.PrisonerMain;
import com.sinog.core.model.UploadAppendixFile;
import com.sinog.core.util.JsonUtil;
import com.sinog.core.util.PageReturnUtil;
import com.sinog.core.utilbean.Constants;
import com.sinog.core.utilbean.Result;
import com.sinog.core.utilbean.ReturnCode;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @desc 户籍迁入迁出实现类
 * @author sunsf
 * @date 2020-02-07-16:40
 */
@Slf4j
@Service
public class BjdxMvInfoMainServiceImpl extends ServiceImpl<AbMvMapper,AbMvResultVo> implements BjdxMvInfoMainService {

    @Resource
    private AbMvMapper abMvMapper;
    @Resource
    private PrisonerMainMapper prisonerMainMapper;
    @Resource
    private PrisonerFxMapper prisonerFxMapper;
    @Resource
    private SystemCodeMapper systemCodeMapper;
    @Autowired
    private SentenceService sentenceService;
    @Autowired
    @Qualifier("middleJdbcTemplate")
    private JdbcTemplate middleJdbcTemplate;
    @Autowired
    private AbXiaozuMapper abXiaozuMapper;
    @Resource
    private UploadAppendixFileMapper uploadAppendixFileMapper;
    @Resource
    private AbAnzhiMapper abAnzhiMapper;
    @Resource
    private AbPrisonerMapper abPrisonerMapper;
    @Resource
    private AbJiuMapper abJiuMapper;
    @Resource
    private AbBangfuMapper abBangfuMapper;
    @Resource
    private AbGenzongbangjiaoMapper abGenzongbangjiaoMapper;
    @Resource
    private AbLieguanMapper abLieguanMapper;
    @Resource
    private AbShehuijiuzhuMapper abShehuijiuzhuMapper;
    @Resource
    private AbZyzjiuzhuMapper abZyzjiuzhuMapper;
    @Resource
    private AbPaichaMapper abPaichaMapper;
    @Resource
    private AbCunJiandingMapper abCunJiandingMapper;
    @Resource
    private ReleaseHelpEduMapper releaseHelpEduMapper;
    @Resource
    private AbTuoguanMapper abTuoguanMapper;
    @Resource
    private PlacementBaseInfoMapper placementBaseInfoMapper;

    /**
     * 安置帮教管理 - 帮教对象信息管理 - 户籍迁入人员管理 - 列表
     * @param pageIndex pageIndex
     * @param pageSize pageSize
     * @param querykey querykey
     * @param departid departid
     * @param level level
     * @param orgid orgid
     * @param sortField 排序字段
     * @param sortOrder 排序方式
     * @return String
     */
    @Override
    public String getHjqrglList(Integer pageIndex,Integer pageSize,@Converse(caseIf = "-1", converseTo = "0")
                                        String querykey,String departid,int level,String orgid,String sortField,String sortOrder) {
        Page<Map<String,String>> page = new Page<>(pageIndex + 1,pageSize);
        // 将当前省份的localCode放在前台
        String localCode = CodeUrlUtils.getLocalCode();
        page = page.setRecords(abMvMapper.getHjqrglList(page,querykey,departid,level,orgid,sortField,sortOrder,localCode));
        return PageReturnUtil.pageReturn(page);
    }

    /**
     * 安置帮教管理 - 帮教对象信息管理 - 户籍迁出人员管理 - 列表
     * @param pageIndex pageIndex
     * @param pageSize pageSize
     * @param querykey querykey
     * @param departid departid
     * @param level level
     * @param orgid orgid
     * @param sortField 排序字段
     * @param sortOrder 排序方式
     * @return String
     */
    @Override
    public String getHjqcglList(Integer pageIndex,Integer pageSize,@Converse(caseIf = "-1", converseTo = "0")
                                        String querykey,String departid,int level,String orgid,String sortField,String sortOrder) {
        Page<Map<String,String>> page = new Page<>(pageIndex + 1,pageSize);
        String localCode = CodeUrlUtils.getLocalCode();
        page = page.setRecords(abMvMapper.getHjqcglList(page,querykey,departid,level,orgid,sortField,sortOrder,localCode));
        return PageReturnUtil.pageReturn(page);
    }

    /**
     * 获取户籍迁出人员信息
     * @param priNum priNum
     * @return Result
     */
    @Override
    public Result getMvBaseInfoByPriNum(String priNum) {
        if(StringUtils.isNotBlank(priNum)) {
            Map<String,String> abp = abMvMapper.getBaseInfoByPriNum(priNum);
            if(null != abp && !abp.isEmpty()) {
                return Result.success(abp);
            }
        }
        return Result.failure(ReturnCode.SELECT_FAILED);
    }

    /**
     * 安置帮教管理 - 帮教对象信息管理 - 户籍迁出人员管理 - 迁移办理 - 姓名搜索
     * @param key key
     * @param departid departid
     * @param level level
     * @return List<Map < String,Object>>
     */
    @Override
    public List<Map<String,Object>> getPrisonerList(String key,String departid,int level) {
        return abPrisonerMapper.getPrisonerListByKey(key,departid,level);
    }

    /**
     * 安置帮教管理 - 帮教对象信息管理 - 户籍迁出人员管理 - 迁移办理 - 保存 (跨省需要数据同步)
     * @param dataStr dataStr
     * @return Result
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result saveBaseInfo(String dataStr) {
        //接收参数实体
        AbMvResultVo prisoner = JsonUtil.toBean(dataStr,AbMvResultVo.class);
        String priNum1 = prisoner.getPriNum();
        QueryWrapper<AbMvResultVo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("pri_num",priNum1);
        //数据库实体
        AbMvResultVo oldPrisoner = abMvMapper.selectOne(queryWrapper);
        if(null != oldPrisoner) {
            prisoner.setId(oldPrisoner.getId());
        } else if(StringUtils.isBlank(prisoner.getId())) {
            prisoner.setId(IdGen.strid());
        }
        // 如果迁入地审批成功需要将原单位的安置信息清空
        if(Constants.NumberStr.NUM_STR_SIX.equals(prisoner.getState()) ||
                Constants.NumberStr.NUM_STR_EIGHT.equals(prisoner.getState())) {
            // 司法局审批通过后将基地的安置人数-1,历史安置人数+1
            List<Map<String,String>> jds = abAnzhiMapper.searchAnzhiJd(priNum1);
            if(0 < jds.size()) {
                jds.forEach(e -> {
                    String jdId = e.get(Constants.StrFinalValue.JDID);
                    if(!StringUtils.isEmpty(jdId)) {
                        //获取安置基地的数量
                        Integer num = placementBaseInfoMapper.getJdKeAnZhiNum(jdId);
                        placementBaseInfoMapper.changeJdNum(e.get("jdid"),--num);
                        //修改历史安置人数
                        PlacementBaseInfo placementBaseInfo = new PlacementBaseInfo();
                        PlacementBaseInfo placement = placementBaseInfo.selectById(jdId);
                        if(placement != null) {
                            int lsAnzhi = Integer.parseInt(null == placement.getLsAnzhirenshu() ? "0" : placement.getLsAnzhirenshu());
                            placement.setLsAnzhirenshu(String.valueOf(++lsAnzhi));
                            placement.updateById();
                        }
                    }
                });
            }
            abAnzhiMapper.deleteByAzInfo(priNum1);
        }
        // 山西个性化流程:户籍迁出流程(司法所 - 司法局 - 迁入地司法局 - 司法所)
        {
            String localCode = CodeUrlUtils.getLocalCode();
            if(Constants.XzqhCode.SHAN1XI.equals(localCode) && Constants.NumberStr.NUM_STR_EIGHT.equals(prisoner.getState())) {
                prisoner.insertOrUpdate();
                Map<String, Object> map = abMvMapper.getInfoBypId(prisoner.getPriNum());
                abPrisonerMapper.updateBaseInfo(map, prisoner.getPriNum());
                //户籍迁入地司法局审批通过 - 需要把主表的户籍信息更新成新的，然后再同步到原省份
                PrisonerMain prisonerMain = prisonerMainMapper.selectPrisonerMain(prisoner.getPriNum());
                prisonerMain.setNativepro((String) map.get("nativepro"));
                prisonerMain.setNativecity((String) map.get("nativecity"));
                prisonerMain.setNativecou((String) map.get("nativecou"));
                prisonerMain.setNativevill((String) map.get("nativevill"));
                prisonerMain.setNativeproName((String) map.get("nativeproname"));
                prisonerMain.setNativecityName((String) map.get("nativecityname"));
                prisonerMain.setNativecouName((String) map.get("nativecouname"));
                prisonerMain.setNativevillName((String) map.get("nativevillname"));
                prisonerMain.setNativenum((String) map.get("nativenum"));
                prisonerMain.setIdNum(Sm4.decryptEcb(prisonerMain.getIdNum()));
                prisonerMain.insertOrUpdate();
                return Result.success(Constants.FriendlyTips.SAVE_SUCCESS);
            }
        }
        //迁出地审批通过
        if(Constants.NumberStr.NUM_STR_TWO.equals(prisoner.getState())) {
            prisoner.insertOrUpdate();
            //{"updateUser":"bjsdcqsfjadmin","pid":"1101012006000001","id":"110000248864802127872","state":"2","thsy":""}
            //1、审批完成 判断迁移后到省份是否是本省，如果是什么也不做，如果不是调用目标省份的接口将ab_mv、ab_prisoners、prisoner_fx
            //三张表同步到目标省份
            //2、判断ab_mv表的NativeInpro是否和本地配置文件的code相等，相等说明户籍迁出的是本省，不相等迁移目标省份不是本省
            String abMvId = prisoner.getId();
            AbMvResultVo abMvResultVo = prisoner.selectById(abMvId);
            String nativeinpro = abMvResultVo.getNativeinpro();
            //本系统的本地code
            String localCode = CodeUrlUtils.getLocalCode();
            boolean mark = StringUtils.equals(localCode,nativeinpro);
            //户籍迁移到外省
            if(!mark) {
                String priNum = prisoner.getPriNum();
                //户籍迁出到其他省
                //1、准备需要同步的数据
                //ab_mv
                String prisonerMainDeleteSql = "delete from prisoner_main where pri_num='" + priNum + "'";
                String prisaveTempDeleteSql = "delete from prisave_temp where pri_num='" + priNum + "'";
                String prisonerFxDeleteSql = "delete from prisoner_fx where pri_num='" + priNum + "'";
                String prisonerHcDeleteSql = "delete from prisoner_hc where pri_num='" + priNum + "'";
                String prisonerSrDeleteSql = "delete from prisoner_sr where pri_num='" + priNum + "'";
                String prisonerZnDeleteSql = "delete from prisoner_zn where pri_num='" + priNum + "'";
                String prisonerZzDeleteSql = "delete from prisoner_zz where pri_num='" + priNum + "'";
                String uploadAppendixFileDeleteSql = "delete from upload_appendix_file where pri_num='" + priNum + "'";
                String abMvDeleteSql = "delete from ab_mv where pri_num ='" + priNum + "'" + " and id='" + prisoner.getId() + "'" + " and state='2'";
                String prisonersJxjsDeleteSql = "delete from prisoners_jxjs where pri_num='" + priNum + "'";
                String prisonersZyjnDeleteSql = "delete from prisoners_zyjn where pri_num='" + priNum + "'";
                String prisonerCjpgDeleteSql = "delete from prisoner_cjpg where Pri_num='" + priNum + "'";
                String prisonerXjxxDeleteSql = "delete from prisoner_xjxx where pri_num='" + priNum + "'";
                String xjReceiptDeleteSql = "delete from xj_receipt where pri_num='" + priNum + "'";
                String xjReceiptDataDeleteSql = "delete from xj_receipt_data where session_id in (select xjid from xj_receipt where pri_num ='" + priNum + "')";
                String abPrisonersDeleteSql = "delete from ab_prisoners where pri_num='" + priNum + "'";
                List<String> prisonerMainInsertSql = JdbcUtils.getInsertSql("prisoner_main","pri_num",priNum,null,null,null,null);
                List<String> prisaveTempinsertSql = JdbcUtils.getInsertSql("prisave_temp","pri_num",priNum,null,null,null,null);
                List<String> prisonerFxinsertSql = JdbcUtils.getInsertSql("prisoner_fx","pri_num",priNum,null,null,null,null);
                List<String> prisonerHcinsertSql = JdbcUtils.getInsertSql("prisoner_hc","pri_num",priNum,null,null,null,null);
                List<String> prisonerSrinsertSql = JdbcUtils.getInsertSql("prisoner_sr","pri_num",priNum,null,null,null,null);
                List<String> prisonerZninsertSql = JdbcUtils.getInsertSql("prisoner_zn","pri_num",priNum,null,null,null,null);
                List<String> prisonerZzinsertSql = JdbcUtils.getInsertSql("prisoner_zz","pri_num",priNum,null,null,null,null);
                List<String> uploadAppendixFileInsertSql = JdbcUtils.getInsertSql("upload_appendix_file","pri_num",priNum,null,null,null,null);
                List<String> abMvInsertSql = JdbcUtils.getInsertSql("ab_mv","pri_num",priNum,"id",prisoner.getId(),"state","2");
                List<String> prisonersJxjsInsertSql = JdbcUtils.getInsertSql("prisoners_jxjs","pri_num",priNum,null,null,null,null);
                List<String> prisonersZyjnInsertSql = JdbcUtils.getInsertSql("prisoners_zyjn","pri_num",priNum,null,null,null,null);
                List<String> prisonerCjpgInsertSql = JdbcUtils.getInsertSql("prisoner_cjpg","pri_num",priNum,null,null,null,null);
                List<String> prisonerXjxxInsertSql = JdbcUtils.getInsertSql("prisoner_xjxx","pri_num",priNum,null,null,null,null);
                List<String> xjReceiptInsertSql = JdbcUtils.getInsertSql("xj_receipt","pri_num",priNum,null,null,null,null);
                List<String> xjReceiptDataInsertSql = JdbcUtils.getInsertSql("xj_receipt_data","session_id",priNum,null,null,null,null);
                List<String> abPrisonersInsertSql = JdbcUtils.getInsertSql("ab_prisoners","pri_num",priNum,null,null,null,null);
                String middleSql1 = JdbcUtils.createMiddleSql(priNum,prisonerMainDeleteSql,Constants.Number.NUM_ZERO,localCode,nativeinpro);
                middleJdbcTemplate.update(middleSql1);
                String middleSql2 = JdbcUtils.createMiddleSql(priNum,prisaveTempDeleteSql,Constants.Number.NUM_ZERO,localCode,nativeinpro);
                middleJdbcTemplate.update(middleSql2);
                String middleSql3 = JdbcUtils.createMiddleSql(priNum,prisonerFxDeleteSql,Constants.Number.NUM_ZERO,localCode,nativeinpro);
                middleJdbcTemplate.update(middleSql3);
                String middleSql4 = JdbcUtils.createMiddleSql(priNum,prisonerHcDeleteSql,Constants.Number.NUM_ZERO,localCode,nativeinpro);
                middleJdbcTemplate.update(middleSql4);
                String middleSql5 = JdbcUtils.createMiddleSql(priNum,prisonerSrDeleteSql,Constants.Number.NUM_ZERO,localCode,nativeinpro);
                middleJdbcTemplate.update(middleSql5);
                String middleSql6 = JdbcUtils.createMiddleSql(priNum,prisonerZnDeleteSql,Constants.Number.NUM_ZERO,localCode,nativeinpro);
                middleJdbcTemplate.update(middleSql6);
                String middleSql7 = JdbcUtils.createMiddleSql(priNum,prisonerZzDeleteSql,Constants.Number.NUM_ZERO,localCode,nativeinpro);
                middleJdbcTemplate.update(middleSql7);
                String middleSql8 = JdbcUtils.createMiddleSql(priNum,uploadAppendixFileDeleteSql,Constants.Number.NUM_ZERO,localCode,nativeinpro);
                middleJdbcTemplate.update(middleSql8);
                String middleSql9 = JdbcUtils.createMiddleSql(priNum,abMvDeleteSql,Constants.Number.NUM_ZERO,localCode,nativeinpro);
                middleJdbcTemplate.update(middleSql9);
                String middleSql10 = JdbcUtils.createMiddleSql(priNum,prisonersJxjsDeleteSql,Constants.Number.NUM_ZERO,localCode,nativeinpro);
                middleJdbcTemplate.update(middleSql10);
                String middleSql11 = JdbcUtils.createMiddleSql(priNum,prisonersZyjnDeleteSql,Constants.Number.NUM_ZERO,localCode,nativeinpro);
                middleJdbcTemplate.update(middleSql11);
                String middleSql12 = JdbcUtils.createMiddleSql(priNum,prisonerCjpgDeleteSql,Constants.Number.NUM_ZERO,localCode,nativeinpro);
                middleJdbcTemplate.update(middleSql12);
                String middleSql13 = JdbcUtils.createMiddleSql(priNum,prisonerXjxxDeleteSql,Constants.Number.NUM_ZERO,localCode,nativeinpro);
                middleJdbcTemplate.update(middleSql13);
                String middleSql14 = JdbcUtils.createMiddleSql(priNum,xjReceiptDeleteSql,Constants.Number.NUM_ZERO,localCode,nativeinpro);
                middleJdbcTemplate.update(middleSql14);
                String middleSql15 = JdbcUtils.createMiddleSql(priNum,xjReceiptDataDeleteSql,Constants.Number.NUM_ZERO,localCode,nativeinpro);
                middleJdbcTemplate.update(middleSql15);
                String middleSql16 = JdbcUtils.createMiddleSql(priNum,abPrisonersDeleteSql,Constants.Number.NUM_ZERO,localCode,nativeinpro);
                middleJdbcTemplate.update(middleSql16);
                for(String sql : prisonerMainInsertSql) {
                    String isql = JdbcUtils.createMiddleSql(priNum,sql,Constants.Number.NUM_ZERO,localCode,nativeinpro);
                    middleJdbcTemplate.update(isql);
                }
                for(String sql : prisaveTempinsertSql) {
                    String isql = JdbcUtils.createMiddleSql(priNum,sql,Constants.Number.NUM_ZERO,localCode,nativeinpro);
                    middleJdbcTemplate.update(isql);
                }
                for(String sql : prisonerFxinsertSql) {
                    String isql = JdbcUtils.createMiddleSql(priNum,sql,Constants.Number.NUM_ZERO,localCode,nativeinpro);
                    middleJdbcTemplate.update(isql);
                }
                for(String sql : prisonerHcinsertSql) {
                    String isql = JdbcUtils.createMiddleSql(priNum,sql,Constants.Number.NUM_ZERO,localCode,nativeinpro);
                    middleJdbcTemplate.update(isql);
                }
                for(String sql : prisonerSrinsertSql) {
                    String isql = JdbcUtils.createMiddleSql(priNum,sql,Constants.Number.NUM_ZERO,localCode,nativeinpro);
                    middleJdbcTemplate.update(isql);
                }
                for(String sql : prisonerZninsertSql) {
                    String isql = JdbcUtils.createMiddleSql(priNum,sql,Constants.Number.NUM_ZERO,localCode,nativeinpro);
                    middleJdbcTemplate.update(isql);
                }
                for(String sql : prisonerZzinsertSql) {
                    String isql = JdbcUtils.createMiddleSql(priNum,sql,Constants.Number.NUM_ZERO,localCode,nativeinpro);
                    middleJdbcTemplate.update(isql);
                }
                for(String sql : uploadAppendixFileInsertSql) {
                    String isql = JdbcUtils.createMiddleSql(priNum,sql,Constants.Number.NUM_ZERO,localCode,nativeinpro);
                    middleJdbcTemplate.update(isql);
                }
                for(String sql : abMvInsertSql) {
                    String isql = JdbcUtils.createMiddleSql(priNum,sql,Constants.Number.NUM_ZERO,localCode,nativeinpro);
                    middleJdbcTemplate.update(isql);
                }
                for(String sql : prisonersJxjsInsertSql) {
                    String isql = JdbcUtils.createMiddleSql(priNum,sql,Constants.Number.NUM_ZERO,localCode,nativeinpro);
                    middleJdbcTemplate.update(isql);
                }
                for(String sql : prisonersZyjnInsertSql) {
                    String isql = JdbcUtils.createMiddleSql(priNum,sql,Constants.Number.NUM_ZERO,localCode,nativeinpro);
                    middleJdbcTemplate.update(isql);
                }
                for(String sql : prisonerCjpgInsertSql) {
                    String isql = JdbcUtils.createMiddleSql(priNum,sql,Constants.Number.NUM_ZERO,localCode,nativeinpro);
                    middleJdbcTemplate.update(isql);
                }
                for(String sql : prisonerXjxxInsertSql) {
                    String isql = JdbcUtils.createMiddleSql(priNum,sql,Constants.Number.NUM_ZERO,localCode,nativeinpro);
                    middleJdbcTemplate.update(isql);
                }
                for(String sql : xjReceiptInsertSql) {
                    String isql = JdbcUtils.createMiddleSql(priNum,sql,Constants.Number.NUM_ZERO,localCode,nativeinpro);
                    middleJdbcTemplate.update(isql);
                }
                for(String sql : xjReceiptDataInsertSql) {
                    String isql = JdbcUtils.createMiddleSql(priNum,sql,Constants.Number.NUM_ZERO,localCode,nativeinpro);
                    middleJdbcTemplate.update(isql);
                }
                for(String sql : abPrisonersInsertSql) {
                    String isql = JdbcUtils.createMiddleSql(priNum,sql,Constants.Number.NUM_ZERO,localCode,nativeinpro);
                    middleJdbcTemplate.update(isql);
                }
            }
            return Result.success(Constants.FriendlyTips.SAVE_SUCCESS);
        } else if(Constants.NumberStr.NUM_STR_SIX.equals(prisoner.getState())) {
            //审批完成
            prisoner.insertOrUpdate();
            Map<String,Object> map = abMvMapper.getInfoBypId(prisoner.getPriNum());
            abPrisonerMapper.updateBaseInfo(map,prisoner.getPriNum());
            //户籍迁入地司法局审批通过 - 需要把主表的户籍信息更新成新的，然后再同步到原省份
            PrisonerMain prisonerMain = prisonerMainMapper.selectPrisonerMain(prisoner.getPriNum());
            prisonerMain.setUpdateUser(prisoner.getUpdateUser());
            prisonerMain.setUpdateTime(new Date());
            prisonerMain.setNativepro((String)map.get("nativepro"));
            prisonerMain.setNativecity((String)map.get("nativecity"));
            prisonerMain.setNativecou((String)map.get("nativecou"));
            prisonerMain.setNativevill((String)map.get("nativevill"));
            prisonerMain.setNativeproName((String)map.get("nativeproname"));
            prisonerMain.setNativecityName((String)map.get("nativecityname"));
            prisonerMain.setNativecouName((String)map.get("nativecouname"));
            prisonerMain.setNativevillName((String)map.get("nativevillname"));
            prisonerMain.setNativenum((String)map.get("nativenum"));
            prisonerMain.setIdNum(Sm4.decryptEcb(prisonerMain.getIdNum()));
            prisonerMain.insertOrUpdate();
            //户籍迁出审批流程完成后，删除原户籍的小组成员
            abXiaozuMapper.deleteXiaozu(prisoner.getPriNum());
            return Result.success(Constants.FriendlyTips.SAVE_SUCCESS);
        } else if((Constants.NumberStr.NUM_STR_FIVE.equals(prisoner.getState()))) {
            // 迁入地司法所申请审批
            prisoner.insertOrUpdate();
            return Result.success(Constants.FriendlyTips.SAVE_SUCCESS);
        } else if(Constants.NumberStr.NUM_STR_THREE.equals(prisoner.getState())) {
            // 迁出地司法部不同意迁出
            // 发起时删除以前迁移的附件
            UploadAppendixFile uploadAppendixFile = new UploadAppendixFile();
            uploadAppendixFile.setPriNum(prisoner.getPriNum());
            uploadAppendixFile.setTaskId(Constants.NumberStr.NUM_STR_TEN_THREE);
            uploadAppendixFileMapper.removeAppendixFilByTaskId(uploadAppendixFile);
            prisoner.insertOrUpdate();
            return Result.success(Constants.FriendlyTips.SAVE_SUCCESS);
        } else if((Constants.NumberStr.NUM_STR_SEVEN.equals(prisoner.getState()))) {
            // 迁入第司法局不通过
            // 发起时删除以前迁移的附件
            UploadAppendixFile uploadAppendixFile = new UploadAppendixFile();
            uploadAppendixFile.setPriNum(prisoner.getPriNum());
            uploadAppendixFile.setTaskId(Constants.NumberStr.NUM_STR_TEN_THREE);
            uploadAppendixFileMapper.removeAppendixFilByTaskId(uploadAppendixFile);
            prisoner.insertOrUpdate();
            return Result.success(Constants.FriendlyTips.SAVE_SUCCESS);
        }
        // 前台赋值,state为1则默认为申请状态
        boolean flag = prisoner.insertOrUpdate();
        if(!flag) {
            return Result.failure(ReturnCode.SAVE_FAILED);
        }
        return Result.success(Constants.FriendlyTips.SAVE_SUCCESS);
    }

    /**
     * 户籍迁出、双列管信息 - 查看 - 基本信息
     * @param priNum priNum
     * @return Result
     */
    @Override
    public Result getBaseInfoViewByPriNum(String priNum) {
        Map<String,Object> map = prisonerMainMapper.getBaseInfoViewByPriNumLg(priNum);
        if(null != map && !map.isEmpty()) {
            return Result.success(map);
        }
        return Result.failure(ReturnCode.SELECT_FAILED);
    }

    /**
     * 户籍迁出信息 - 查看 - 多媒体信息
     * @param priNum priNum
     * @return Result
     */
    @Override
    public Result getDmtInfoViewByPriNum(String priNum) {
        Map<String,Object> map = new HashMap<>(16);
        //获取头像列表
        List<Map<String,Object>> headList = uploadAppendixFileMapper.getDmtInfoViewByPriNum(priNum,"2");
        //获取指纹列表
        List<Map<String,Object>> fingerList = uploadAppendixFileMapper.getDmtInfoViewByPriNum(priNum,"3");
        map.put("headList",headList);
        map.put("fingerList",fingerList);
        if(!StringUtils.isEmpty(priNum)) {
            //服刑人员基本信息
            PrisonerMain prisonerMain = prisonerMainMapper.selectById(priNum);
            //姓名
            map.put("pname",null != prisonerMain.getPName() ? prisonerMain.getPName() : "无");
            //证件号
            map.put("idnum",null != prisonerMain.getIdNum() ? prisonerMain.getIdNum() : "无");
        }
        if(!map.isEmpty()) {
            return Result.success(map);
        }
        return Result.failure(ReturnCode.SELECT_FAILED);
    }

    /**
     * 户籍迁出信息 - 查看 - 服刑信息
     * @param priNum priNum
     * @return Result
     */
    @Override
    public Result getFxInfoViewByPriNum(String priNum) {
        Map<String,Object> map = prisonerFxMapper.getFxInfoViewByPriNum(priNum);
        String critype = prisonerFxMapper.selectByPriNum(priNum);
        List<Map<String,Object>> dataList;
        if(null != critype && !StringUtils.isEmpty(critype) && null != map) {
            List<Map<String, String>> codeNameList = systemCodeMapper.getCodeNameByCodeId(Arrays.asList(critype.split(",")));
            List<String> nameList = new ArrayList<>(16);
            codeNameList.forEach(e -> nameList.add(String.valueOf(e.get("codeName"))));
            String critypeName = StringUtils.join(nameList,',');
            map.put("critype",critypeName);
            String fzlx = prisonerFxMapper.selectFzlxByPriNum(priNum);
            Result codeName = sentenceService.getCodeName(fzlx);
            dataList = (List<Map<String,Object>>)codeName.getData();
            List<String> fzlxList = new ArrayList<>(16);
            dataList.forEach(data -> {
                for(String key : data.keySet()) {
                    if("codeType".equals(key)) {
                        fzlxList.add((String)data.get("codeType"));
                    }
                }
            });
            map.put("aftype",StringUtils.join(fzlxList,","));
        }
        String addpenalty = prisonerFxMapper.selectAddenaltyByPriNum(priNum);
        PrisonerMain prisonerMain = prisonerMainMapper.selectById(priNum);
        String addpenaltyName;
        if(null != addpenalty && !StringUtils.isEmpty(addpenalty) && null != map) {
            List<String> addpenaltyNameList = systemCodeMapper.getAddpenaltyName(Arrays.asList(addpenalty.split(",")));
            addpenaltyName = StringUtils.join(addpenaltyNameList,',');
            map.put("addpenalty",addpenaltyName);
        }
        if(null != prisonerMain && null != map) {
            map.put("pname",prisonerMain.getPName());
            map.put("idnum",prisonerMain.getIdNum());
            map.put("pdanweiname",prisonerMain.getPdanweiName());
        }
        if(null != map && !map.isEmpty()) {
            return Result.success(map);
        }
        return Result.failure(ReturnCode.SELECT_FAILED);
    }

    /**
     * 户籍迁出信息 - 查看 - 核查记录信息
     * @param priNum priNum
     * @return Result
     */
    @Override
    public Result getHcjlInfoViewByPriNum(String priNum) {
        Map<String,Object> map = prisonerMainMapper.getHcjlInfoViewByPriNum(priNum);
        if(null != map && !map.isEmpty()) {
            return Result.success(map);
        }
        return Result.failure(ReturnCode.SELECT_FAILED);
    }

    /**
     * 户籍迁出信息 - 查看 - 档案材料信息
     * @param priNum priNum
     * @return Result
     */
    @Override
    public Result getDaclInfoViewByPriNum(String priNum) {
        Map<String,Object> map = prisonerMainMapper.getDaclInfoViewByPriNum(priNum);
        if(null != map && !map.isEmpty()) {
            return Result.success(map);
        }
        return Result.failure(ReturnCode.SELECT_FAILED);
    }

    /**
     * 户籍迁出信息 - 查看 - 安置帮教信息
     * @param priNum priNum
     * @return Result
     */
    @Override
    public Result getAzbjInfoViewByPriNum(String priNum) {
        Map<String,Object> result = new HashMap<>(16);
        //获取本省份code前两位
        String flag = CodeUrlUtils.getLocalCodeStartTwo();
        //获取安置信息
        List<Map<String,Object>> azList = abAnzhiMapper.getAzBaseInfonAgainList(priNum,flag);
        result.put("azList",azList);
        //获取救助信息
        List<Map<String,Object>> jzList = abJiuMapper.getJzInfoViewByPriNum(priNum);
        result.put("jzList",jzList);
        //获取帮教信息
        List<Map<String,Object>> bjList = abBangfuMapper.getBjInfoViewByPriNum(priNum);
        //帮教其他信息
        List<Map<String,Object>> bjResultList = new ArrayList<>(16);
        for(Map<String,Object> map : bjList) {
            String pp = (String)map.get("pp");
            //帮扶附件
            List<Map<String,Object>> appendixBjList = uploadAppendixFileMapper.getUploadAppendixFile(pp,Constants.AppendixFile.APDIX_FILE_TEN_FOUR,null);
            map.put("appendixBjList",appendixBjList);
            //获取地方党政
            Map<String,Object> mapDz = abGenzongbangjiaoMapper.getBjDzBaseInfoRemark(pp);
            if(null != mapDz) {
                map.putAll(mapDz);
                //附件
                List<Map<String,Object>> appendixBjDzList = uploadAppendixFileMapper.getUploadAppendixFile(pp,Constants.AppendixFile.APDIX_FILE_TEN_FIVE,null);
                map.put("appendixBjDzList",appendixBjDzList);
            }
            //社会组织
            Map<String,Object> mapShzz = abShehuijiuzhuMapper.getBjShzzBaseInfoNew(pp);
            if(null != mapShzz) {
                map.putAll(mapShzz);
                //附件
                List<Map<String,Object>> appendixBjShzzList = uploadAppendixFileMapper.getUploadAppendixFile(pp,Constants.AppendixFile.APDIX_FILE_TEN_SEVEN,null);
                map.put("appendixBjShzzList",appendixBjShzzList);
            }
            //志愿者救助
            Map<String,Object> mapZyz = abZyzjiuzhuMapper.getBjZyzBaseInfoNew(pp);
            if(null != mapZyz) {
                map.putAll(mapZyz);
                //附件
                List<Map<String,Object>> appendixBjZyzList = uploadAppendixFileMapper.getUploadAppendixFile(pp,Constants.AppendixFile.APDIX_FILE_TEN_EIGHT,null);
                map.put("appendixBjZyzList",appendixBjZyzList);
            }
            //排查
            Map<String,Object> mapPc = abPaichaMapper.getBjPcBaseInfoNew(pp);
            if(null != mapPc) {
                map.putAll(mapPc);
                //附件
                List<Map<String,Object>> appendixBjPcList = uploadAppendixFileMapper.getUploadAppendixFile(pp,"19",null);
                map.put("appendixBjPcList",appendixBjPcList);
            }
            bjResultList.add(map);
        }
        result.put("bjResultList",bjResultList);
        //获取年度鉴定信息
        List<Map<String,Object>> jdList = abCunJiandingMapper.getNdjdInfoViewByPriNum(priNum);
        result.put("jdList",jdList);
        //获取解除帮教信息
        List<Map<String,Object>> jcbjList = releaseHelpEduMapper.getJcbjInfoViewByPriNum(priNum);
        result.put("jcbjList",jcbjList);
        //获取解除失联信息
        List<Map<String,Object>> slList = abTuoguanMapper.getSlInfoViewByPriNum(priNum);
        result.put("slList",slList);
        return Result.success(result);
    }

    /**
     * 户籍迁出信息 - 查看 - 双列管信息
     * @param priNum priNum
     * @return Result
     */
    @Override
    public Result getSlgInfoViewByPriNum(String priNum) {
        Map<String,Object> map = abLieguanMapper.getSlgInfoViewByPriNum(priNum);
        if(null != map && !map.isEmpty()) {
            return Result.success(map);
        }
        return Result.failure(ReturnCode.SELECT_FAILED);
    }

    /**
     * 安置帮教管理 - 帮教对象信息管理 - 户籍迁出人员管理 - 撤回
     * @param id id
     * @param state 审批状态:0:司法所迁出撤回；1:迁出审批中；2:迁出审批成功；3:迁出审批失败；4:司法所迁入撤回；5:迁入审批中；6:迁入审批成功；7:迁入审批失败；
     * @return Result
     */
    @Override
    public Result toRevoke(String id,String state) {
        int num = abMvMapper.updateMvById(id,state);
        if(0 < num) {
            return Result.success("撤回成功");
        }
        return Result.failure(ReturnCode.SAVE_FAILED);
    }

    /**
     * 户籍迁出信息 - 获取迁移信息
     * @param id id
     * @return Result
     */
    @Override
    public Result getMvInfoById(String id) {
        Map<String,Object> map = abMvMapper.getMvInfoById(id);
        String priNum = "";
        if(null != map && map.containsKey(Constants.StrFinalValue.PRINUMUC)) {
            priNum = (String)map.get(Constants.StrFinalValue.PRINUMUC);
        }
        Map<String,String> abp = abMvMapper.getBaseInfoByPriNum(priNum);
        assert map != null;
        abp.put("idnum",Sm4.decryptEcb(abp.get("idnum")));
        map.putAll(abp);
        if(!map.isEmpty()) {
            return Result.success(map);
        }
        return Result.failure(ReturnCode.SELECT_FAILED);
    }

    /**
     * 户籍迁出 - 查看
     * @param id id
     * @return Result
     */
    @Override
    public Result getMvBaseInfoForCheck(String id) {
        if(null == id) {
            return Result.failure(ReturnCode.SELECT_FAILED);
        }
        Map<String,String> abp = abMvMapper.getMvBaseInfoForCheck(id);
        if(null != abp && !abp.isEmpty()) {
            return Result.success(abp);
        }
        return Result.failure(ReturnCode.SELECT_FAILED);
    }
}